home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / du_lib / resource.c < prev    next >
C/C++ Source or Header  |  1995-07-10  |  15KB  |  497 lines

  1. /*
  2.    DU_LIB v2
  3.    Gem Window Management & Dialog Library For Lattice C
  4.    ½1994,95, by Craig Graham.
  5.    Based on the DU_LIBv1 Library for HiSoft Basic.
  6. */
  7.  
  8. #include <FCNTL.H>
  9. #include <STDIO.H>
  10. #include <OSBIND.H>
  11. #include "DULIB.H"
  12.  
  13. /*
  14.     RESOURCE FILE HANDLER
  15. */
  16.  
  17. /* Convert a colour drawn form into a mono form */
  18. void rsrc_form2mono(short d, short b)
  19. {
  20.     OBJECT *a;
  21.     OBJECT *o;
  22.     TEDINFO *t;
  23.     short obn,bgc,cw;
  24.     unsigned long c;
  25.     ICONBLK *ico;
  26.     
  27.     bgc=b<<4;
  28.     
  29.     rsrc_gaddr(0,d,&a);
  30.  
  31.     obn=0;
  32.     do {
  33.         o=a+obn;
  34.         switch (o->ob_type)
  35.         {
  36.             case G_BOXCHAR:
  37.                 t=(TEDINFO*)o->ob_spec;
  38.                 c=(unsigned long)o->ob_spec;
  39.                 cw=(short)c&65535;
  40.                 c=(unsigned long)(c&(unsigned long)(4294967295-65535));        // Preserve the character & border info.
  41.                 if ((cw&15)==0)
  42.                 {
  43.                     cw=cw&((15<<12)+(1<<7)+(7<<4));    // Mask out text colour & set fill colour to 0
  44.                     cw=cw+bgc+1;
  45.                 }else{
  46.                     cw=cw&((15<<12)+(1<<7)+(7<<4));    // Mask out text colour & set fill colour to 0
  47.                 }
  48.                 cw=cw+(1<<8);                        // Text colour to black
  49.                 o->ob_spec=(void*)(c+cw);
  50.                 /* Using 3d effects to highlight things is turned off for mono */
  51.                 if (o->ob_flags & mask_indicator) o->ob_flags=o->ob_flags - mask_indicator;
  52.                 if (o->ob_flags & mask_activator) o->ob_flags=o->ob_flags - mask_activator;
  53.                 break;
  54.             case G_TEXT:
  55.                 t=(TEDINFO*)o->ob_spec;
  56.                 cw=t->te_color;
  57.                 cw=cw&((15<<12)+(1<<7)+(7<<4));    // Mask out text colour & set fill colour to 0
  58.                 cw=cw+(1<<8);                        // Text colour to black
  59.                 t->te_color=cw;
  60.                 /* Using 3d effects to highlight things is turned off for mono */
  61.                 if (o->ob_flags & mask_indicator) o->ob_flags=o->ob_flags - mask_indicator;
  62.                 if (o->ob_flags & mask_activator) o->ob_flags=o->ob_flags - mask_activator;
  63.                 break;
  64.             case G_BOXTEXT:
  65.                 t=(TEDINFO*)o->ob_spec;
  66.                 cw=t->te_color;
  67.                 if ((cw&15)==0)
  68.                 {
  69.                     cw=cw&((15<<12)+(1<<7)+(7<<4));    // Mask out text colour & set fill colour to 0
  70.                     cw=cw+bgc+1;
  71.                 } else {
  72.                     cw=cw&((15<<12)+(1<<7)+(7<<4));    // Mask out text colour & set fill colour to 0
  73.                 }
  74.                 cw=cw+(1<<8);                        // Text colour to black
  75.                 t->te_color=cw;
  76.                 /* Using 3d effects to highlight things is turned off for mono */
  77.                 if (o->ob_flags & mask_indicator) o->ob_flags=o->ob_flags - mask_indicator;
  78.                 if (o->ob_flags & mask_activator) o->ob_flags=o->ob_flags - mask_activator;
  79.                 break;
  80.             case G_FTEXT:
  81.                 t=(TEDINFO*)o->ob_spec;
  82.                 cw=t->te_color;
  83.                 cw=cw&((15<<12)+(1<<7)+(7<<4));    // Mask out text colour & set fill colour to 0
  84.                 cw=cw+(1<<8);                        // Text colour to black
  85.                 t->te_color=cw;
  86.                 /* Using 3d effects to highlight things is turned off for mono */
  87.                 if (o->ob_flags & mask_indicator) o->ob_flags=o->ob_flags - mask_indicator;
  88.                 if (o->ob_flags & mask_activator) o->ob_flags=o->ob_flags - mask_activator;
  89.                 break;
  90.             case G_FBOXTEXT:
  91.                 t=(TEDINFO*)o->ob_spec;
  92.                 cw=t->te_color;
  93.                 if ((cw&15)==0)
  94.                 {
  95.                     cw=cw&((15<<12)+(1<<7)+(7<<4));    // Mask out text colour & set fill colour to 0
  96.                     cw=cw+bgc+1;
  97.                 } else {
  98.                     cw=cw&((15<<12)+(1<<7)+(7<<4));    // Mask out text colour & set fill colour to 0
  99.                 }
  100.                 cw=cw+(1<<8);                        // Text colour to black
  101.                 t->te_color=cw;
  102.                 /* Using 3d effects to highlight things is turned off for mono */
  103.                 if (o->ob_flags & mask_indicator) o->ob_flags=o->ob_flags - mask_indicator;
  104.                 if (o->ob_flags & mask_activator) o->ob_flags=o->ob_flags - mask_activator;
  105.                 break;
  106.             case G_BOX:
  107.                 c=(unsigned long)o->ob_spec;
  108.                 if ((c&240) != 0)
  109.                 {
  110.                     if (((c&15)==8)||(o->ob_flags & (mask_indicator|mask_activator) !=0))
  111.                     {
  112.                         c=(c & !255) & !(61440);
  113.                         if ((o->ob_flags & (mask_indicator|mask_activator))==0) /* Using 3d effects to highlight things is turned off for mono */
  114.                         {
  115.                             c=c | (bgc + 7 + 65536 + 4096);
  116.                         }else{
  117.                             c=c | (7 + 65536 + 4096);
  118.                         }
  119.                         o->ob_spec=(void *)c;
  120.                     }
  121.                 } else {
  122.                     o->ob_state=o->ob_state|OUTLINED;
  123.                 }
  124.                 /* Using 3d effects to highlight things is turned off for mono */
  125.                 o->ob_flags&=~(mask_indicator|mask_activator);
  126.                 break;
  127.             case G_ICON:
  128.                 ico=(ICONBLK*)o->ob_spec;
  129.                 ico->ib_char=4161;                
  130.                 o->ob_flags&=~(mask_indicator|mask_activator);
  131.                 o->ob_state=o->ob_state|OUTLINED;
  132.                 break;
  133.             case G_CICON:
  134.                 o->ob_type=G_ICON;            /* Make the colour icon into a standard mono icon so the old AES can handle it */
  135.                 ico=(ICONBLK*)o->ob_spec;
  136.                 ico->ib_char=4161;
  137.                 o->ob_flags&=~(mask_indicator|mask_activator);
  138.                 o->ob_state=o->ob_state | OUTLINED;
  139.                 break;
  140.         }
  141.         obn++;
  142.     } while (((o->ob_flags) & LASTOB)!=LASTOB);
  143. }
  144.  
  145. /*
  146.     Simulate the standard GEM AES resource access functions.
  147.     I've added these to Steve's routines to act as a bit of an interface
  148.     to his routines.
  149. */
  150.  
  151. void *GEM_RESOURCE=NULL;    /* The address of the single 'GEM' resource file */
  152.  
  153. /* Resource char to pixel coord conversion constants.
  154.    If you are designing your resources in ST-Low res, change DU_RSY_CONV to 8 */
  155. #define DU_RSX_CONV 8
  156. #define DU_RSY_CONV 16
  157.  
  158. int DUrsrc_load(char *n)
  159. {
  160.     GEM_RESOURCE=LoadResources(n, DU_RSX_CONV, DU_RSY_CONV);
  161.     if (GEM_RESOURCE)
  162.         return 1;
  163.     else
  164.         return 0;
  165. }
  166.  
  167. int DUrsrc_free(void)
  168. {
  169.     if (GEM_RESOURCE)
  170.     {
  171.         FreeResources(GEM_RESOURCE);
  172.         return 1;
  173.     }
  174.     return 0;
  175. }
  176.  
  177. /*
  178.     Replacement for rsrc_gaddr
  179.     If you only ever use the find tree mode (R_TREE) of rsrc_gaddr,
  180.     then define RSRC_GADDR_ONLY_TREES to get a slightly faster response.
  181. */
  182. int DUrsrc_gaddr(int type, int index, void *addr)
  183. {
  184.     OBJECT **tree_addr;
  185. #ifndef RSRC_GADDR_ONLY_TREES
  186.     char **text_addr;
  187.     void **image_addr;
  188.  
  189.     switch(type)
  190.     {
  191.         case R_TREE:
  192.             tree_addr=(OBJECT**)addr;
  193.             *tree_addr=ResourceTree(GEM_RESOURCE,index);
  194.             break;
  195.         case R_STRING:
  196.             text_addr=(char**)addr;
  197.             *text_addr=ResourceString(GEM_RESOURCE,index);
  198.             break;
  199.         case R_IMAGEDATA:
  200.             image_addr=(void**)addr;
  201.             *image_addr=ResourceImage(GEM_RESOURCE,index);
  202.             break;
  203.     }
  204. #else
  205.  
  206.     tree_addr=(OBJECT**)addr;
  207.     *tree_addr=ResourceTree(GEM_RESOURCE,index);
  208. #endif
  209.     return 1;
  210. }
  211.  
  212. /*
  213.     Code in this module is based on the resource loader from
  214.     Steve Sowerby's AGiLE library. Thanks to Steve for allowing
  215.     me to use his code.
  216. */
  217.  
  218. short resWidth,resHeight;
  219.  
  220. /*
  221.     FixColourIconData : Convert a colour icon from device independent to device specific
  222. */
  223. void FixColourIconData(CICONBLK *icon)
  224. {
  225.     CICON *c;
  226.     MFDB src,dst;
  227.  
  228.     for(c=icon->mainlist; c; c=c->next_res)
  229.     {
  230.         src.fd_w = icon->monoblk.ib_wicon;                /*Transform standard icon*/
  231.         src.fd_h = icon->monoblk.ib_hicon;
  232.         src.fd_wdwidth = icon->monoblk.ib_wicon>>4;
  233.         src.fd_stand = 1;
  234.         src.fd_r1 = src.fd_r2 = src.fd_r3 = 0;
  235.         src.fd_nplanes = c->num_planes;
  236.         src.fd_addr = c->col_data;
  237.         dst = src;
  238.         dst.fd_stand = 0;
  239.         vr_trnfm(x_handle,&src,&dst);
  240.         if (c->sel_data)                            /*Transform selected icon if exists*/
  241.         {
  242.             src.fd_w = icon->monoblk.ib_wicon;
  243.             src.fd_h = icon->monoblk.ib_hicon;
  244.             src.fd_wdwidth = icon->monoblk.ib_wicon>>4;
  245.             src.fd_stand = 1;
  246.             src.fd_r1 = src.fd_r2 = src.fd_r3 = 0;
  247.             src.fd_nplanes = c->num_planes;
  248.             src.fd_addr = c->sel_data;
  249.             dst = src;
  250.             dst.fd_stand = 0;
  251.             vr_trnfm(x_handle,&src,&dst);
  252.         }
  253.     }
  254. }
  255.  
  256. /*
  257.     LoadResources : Load a GEM resource file
  258.     fname = name of file to load
  259.     Return = base pointer of resources or NULL on failure
  260. */
  261. void *LoadResources(char *fname,short designWidth,short designHeight)
  262. {
  263.     OBJECT dummyObj = {-1,-1,-1,G_BOX,0,0,0L,0,0,1,1};
  264.     long err;
  265.     short handle;
  266.     RSHDR hdr;
  267.     OBJECT *obj,*tree;
  268.     TEDINFO *ti;
  269.     ICONBLK *ib;
  270.     CICONBLK *cib,**cibh;
  271.     BITBLK *bb;
  272.     CICON *cicn,*pcicn;
  273.     unsigned long osize,size,*index,*addr,*earray;
  274.     char *base,*ptext;
  275.     short i,j,type,numCibs=0,numRez,*pdata;
  276.  
  277.     rsrc_obfix(&dummyObj,0);
  278.     resWidth = dummyObj.ob_width;
  279.     resHeight = dummyObj.ob_height;
  280.     
  281.     err = Fopen(fname,O_RDONLY);
  282.     if (err<0L)
  283.         return NULL;
  284.     handle = (short)err;
  285.     Fread(handle,sizeof(RSHDR),&hdr);
  286.     size = (unsigned long)hdr.rsh_rssize;
  287.     osize = (size+1UL)&0xfffffffeUL;
  288.     if (hdr.rsh_vrsn&4)
  289.     { /* Extended format, get new 32-bit length */
  290.         Fseek(osize,handle,SEEK_SET);
  291.         Fread(handle,4L,&size);
  292.     }
  293.     Fseek(0L,handle,SEEK_SET);
  294.     base = (char*)malloc(size+100);
  295.     if (!base)
  296.         return NULL;
  297.     Fread(handle,size,base);
  298.     Fclose(handle);
  299.  
  300.     ti = (TEDINFO*)(base+(unsigned long)hdr.rsh_tedinfo);
  301.     for (i=0;i<hdr.rsh_nted;i++,ti++)
  302.     { /* Correct all tedinfo field pointers */
  303.         ti->te_ptext = (char*)((unsigned long)ti->te_ptext+base);
  304.         ti->te_ptmplt = (char*)((unsigned long)ti->te_ptmplt+base);
  305.         ti->te_pvalid = (char*)((unsigned long)ti->te_pvalid+base);
  306.     }
  307.  
  308.     ib = (ICONBLK*)(base+(unsigned long)hdr.rsh_iconblk);
  309.     for (i=0;i<hdr.rsh_nib;i++,ib++)
  310.     { /* Correct all iconblk field pointers */
  311.         ib->ib_pmask = (short*)((unsigned long)ib->ib_pmask+base);
  312.         ib->ib_pdata = (short*)((unsigned long)ib->ib_pdata+base);
  313.         ib->ib_ptext = (char*)((unsigned long)ib->ib_ptext+base);
  314.     }
  315.  
  316.     bb = (BITBLK*)(base+(unsigned long)hdr.rsh_bitblk);
  317.     for (i=0;i<hdr.rsh_nbb;i++,bb++) /* Correct all bitblk data pointers */
  318.         bb->bi_pdata = (short*)((unsigned long)bb->bi_pdata+base);
  319.     if (hdr.rsh_vrsn&4)
  320.     { /* It's an enhanced RSC file */
  321.  
  322.         earray = (unsigned long*)(osize+(long)base);
  323.  
  324.         cibh = (CICONBLK**)(earray[1]+(long)base);
  325.         if ((long)cibh>0L)
  326.         { /* Get colour icons */
  327.             while (*cibh++!=(CICONBLK*)-1L)
  328.                 numCibs++;
  329.  
  330.             cib = (CICONBLK*)cibh;
  331.             cibh = (CICONBLK**)(earray[1]+(long)base);
  332.             for (i=0;i<numCibs;i++)
  333.             { /* Fix up all the CICONBLK's */
  334.                 cibh[i] = cib;
  335.                 ib = &cib->monoblk;
  336.                 size = 2UL*(unsigned long)(ib->ib_wicon/16)*(unsigned long)ib->ib_hicon;
  337.                 addr = (long*)((long)cib+sizeof(ICONBLK));
  338.                 numRez = (short)*addr;
  339.                 pdata = (short*)&addr[1];
  340.                 ib->ib_pdata = pdata;
  341.                 ib->ib_pmask = (short*)((long)pdata+size);
  342.                 ptext = (char*)((long)pdata+size*2L);
  343.                 ib->ib_ptext = ptext;
  344.                 ptext[11] = 0;
  345.                 cicn = (CICON*)((long)ptext+12L);
  346.                 cib->mainlist = cicn;
  347.                 for (j=0;j<numRez;j++)
  348.                 { /* Get CICON's at different rez's */
  349.                     pcicn = cicn;
  350.                     pdata = (short*)((long)cicn+sizeof(CICON));
  351.                     cicn->col_data = pdata;
  352.                     cicn->col_mask = (short*)((long)pdata+size*(unsigned long)cicn->num_planes);
  353.                     if (cicn->sel_data!=NULL)
  354.                     { /* It's got a selected form */
  355.                         cicn->sel_data = (short*)((long)cicn->col_mask+size);
  356.                         cicn->sel_mask = (short*)((long)cicn->sel_data+size*(unsigned long)cicn->num_planes);
  357.                         cicn = (CICON*)((long)pcicn+sizeof(CICON)+2L*size*((unsigned long)pcicn->num_planes+1L));
  358.                     }
  359.                     else
  360.                     { /* No selected version */
  361.                         cicn->sel_data = cicn->sel_mask = NULL;
  362.                         cicn = (CICON*)((long)pcicn+sizeof(CICON)+size*((unsigned long)pcicn->num_planes+1L));
  363.                     }
  364.                     if (pcicn->next_res==(CICON*)1L)
  365.                         pcicn->next_res = cicn;
  366.                     else
  367.                         pcicn->next_res = NULL;
  368.                 }
  369.                 cib = (CICONBLK*)cicn;
  370.             }
  371.         }
  372.     }
  373.  
  374.     obj = (OBJECT*)(base+(unsigned long)hdr.rsh_object);
  375.     for (i=0;i<hdr.rsh_nobs;i++,obj++)
  376.     { /* Correct all objects' ob_spec pointers */
  377.         type = obj->ob_type&255;
  378.         switch (type)
  379.         { /* What kind of object is it? */
  380.             case G_TEXT:
  381.             case G_BOXTEXT:
  382.             case G_IMAGE:
  383.             case G_BUTTON:
  384.             case G_STRING:
  385.             case G_FTEXT:
  386.             case G_FBOXTEXT:
  387.             case G_ICON:
  388.             case G_TITLE:
  389.                 obj->ob_spec = (void*)((unsigned long)obj->ob_spec+base);
  390.                 break;
  391.             case G_CICON:
  392.                 if ((AESversion<3)||((AESversion==3)&&(AESversion_m<60)))
  393.                     obj->ob_type=G_ICON;
  394.                 else
  395.                     FixColourIconData(cibh[(long)obj->ob_spec]);
  396.                 obj->ob_spec = cibh[(long)obj->ob_spec];
  397.                 break;
  398.             case G_PROGDEF:
  399.                 obj->ob_spec = NULL;
  400.                 break;
  401.             case G_IBOX:
  402.             case G_BOX:
  403.             case G_BOXCHAR:
  404.                 break;
  405.             default:
  406.                 Cconws("Unknown object type\r\n");
  407.                 break;
  408.         }
  409.     }
  410.  
  411.     index = (unsigned long*)(base+(unsigned long)hdr.rsh_trindex);
  412.     for (i=0;i<hdr.rsh_ntree;i++,index++)
  413.     {
  414.         tree = obj = (OBJECT*)(*index+(unsigned long)base);
  415.         if ((obj[3].ob_type&255)!=G_TITLE) /* Not a menu tree */
  416.             do { /* Fix all object coordinates */
  417.                 obj->ob_x = (((obj->ob_x&255)*designWidth+(obj->ob_x>>8))*resWidth)/designWidth;
  418.                 obj->ob_y = (((obj->ob_y&255)*designHeight+(obj->ob_y>>8))*resHeight)/designHeight;
  419.                 obj->ob_width = (((obj->ob_width&255)*designWidth+(obj->ob_width>>8))*resWidth)/designWidth;
  420.                 obj->ob_height = (((obj->ob_height&255)*designHeight+(obj->ob_height>>8))*resHeight)/designHeight;
  421.             } while (!(obj++->ob_flags&LASTOB));
  422.         else
  423.         { /* Standard AES menu */
  424.             j = 0;
  425.             do { /* Use conventional AES routine */
  426.                 rsrc_obfix(tree,j);
  427.             } while (!(tree[j++].ob_flags&LASTOB));
  428.         }
  429.     }
  430.     return (void*)base;
  431. }
  432.  
  433.  
  434. /*
  435.     FreeResources : Dispose of a set of loaded resources
  436.     base = pointer to base of resources
  437. */
  438. void FreeResources(void *base)
  439. {
  440.     free(base);
  441. }
  442.  
  443.  
  444. /*
  445.     ResourceTree : Find the tree with a given index
  446.     base = pointer to base of resources
  447.     num = index number of tree
  448.     Return = pointer to tree or NULL on failure
  449. */
  450. OBJECT *ResourceTree(void *base,short num)
  451. {
  452.     unsigned long *index,offset;
  453.     RSHDR *hdr = (RSHDR*)base;
  454.  
  455.     if ((!hdr)||(num<0)||(num>=hdr->rsh_ntree))
  456.         return NULL;
  457.     index = (unsigned long*)((unsigned long)base+(unsigned long)hdr->rsh_trindex);
  458.     offset = index[num];
  459.     return (OBJECT*)((unsigned long)base+offset);
  460. }
  461.  
  462.  
  463. /* ResourceString : Find the string with a given index
  464.     base = pointer to base of resources
  465.     num = index number of string
  466.     Return = pointer to string or NULL on failure
  467. */
  468. char *ResourceString(void *base,short num)
  469. {
  470.     unsigned long *index,offset;
  471.     RSHDR *hdr = (RSHDR*)base;
  472.  
  473.     if (!(hdr)||(num<0)||(num>=hdr->rsh_nstring))
  474.         return(NULL);
  475.     index = (unsigned long*)((unsigned long)base+(unsigned long)hdr->rsh_frstr);
  476.     offset = index[num];
  477.     return((char*)((unsigned long)base+offset));
  478. }
  479.  
  480. /*
  481.     ResourceImage : Find the image with a given index
  482.     base = pointer to base of resources
  483.     num = index number of image
  484.     Return = pointer to image or NULL on failure
  485. */
  486. void *ResourceImage(void *base,short num)
  487. {
  488.     unsigned long *index,offset;
  489.     RSHDR *hdr = (RSHDR*)base;
  490.  
  491.     if ((!hdr)||(num<0)||(num>=hdr->rsh_nimages))
  492.         return(NULL);
  493.     index = (unsigned long*)((unsigned long)base+(unsigned long)hdr->rsh_frimg);
  494.     offset = index[num];
  495.     return((void*)((unsigned long)base+offset));
  496. }
  497.